//
//  MTVManager.swift
//  MobileRTCSwiftSample
//
//  Created by zhangheng on 2019/6/20.
//  Copyright © 2019 Zoom Video Communications, Inc. All rights reserved.
//

import Foundation

class NetVersion: NSObject {
    
    class func check(suess: @escaping (Bool)->Void) {
        HUD.show()
        NetworkTool.requestData(type: .post, URLString: mt_url_itunes, finishCallback: { (result) in
            HUD.dismiss()
            let resultsArray = ((result as! NSDictionary)["results"] as! NSArray)
            if resultsArray.count > 0 {
                
                let resultsDic = resultsArray[0] as! NSDictionary
                let version =  resultsDic["version"] as! String
                //print(releaseNotes)
                let isok = AppInfo.majorVersion().isOlder(than: version)
                suess(isok)
              
            }
            
        }) { (result) in
            HUD.dismiss()
            show_msg("获取数据失败，检查您的网络，刷新请重试！".localized())
        }
        
    }
    
    
    class func versionUpdate()  {
        
        NetworkTool.requestData(type: .post, URLString: mt_url_itunes, finishCallback: { (result) in
            
            let resultsArray = ((result as! NSDictionary)["results"] as! NSArray)
            if resultsArray.count > 0 {
                let resultsDic = resultsArray[0] as! NSDictionary
                var releaseNotes = "⭐️⭐️修复已知的bug"
                if resultsDic[ "releaseNotes"] != nil {
                      releaseNotes = resultsDic["releaseNotes"] as! String
                }
                let version =  resultsDic["version"] as! String
                //print(releaseNotes)
                var info = VersionInfo()
                info.version = version
                info.message = releaseNotes
                VersionManager.versionUpdate(info: info)
            }
            
        }) { (result) in
            show_msg("获取数据失败，检查您的网络，刷新请重试！".localized())
        }
    }
 
    
    class func checkComp(suess: @escaping (Bool,[String:Any])->Void) {
        HUD.show()
        NetworkTool.requestData(type: .post, URLString: mt_url_macUpdate, finishCallback: { (result) in
            HUD.dismiss()
            
            let version = (result as! NSDictionary)["version"] as? String
            if  version != nil {
                let isok = AppInfo.majorVersion().isOlder(than: version!)
                suess(isok, result as! [String : Any])
            }
            
        }) { (result) in
            HUD.dismiss()
            show_msg("获取数据失败，检查您的网络，刷新请重试！".localized())
        }
        
    }
    
    class func versionCompUpdate()  {
        
        NetworkTool.requestData(type: .post, URLString: mt_url_macUpdate, finishCallback: { (result) in
            
            let version = (result as! NSDictionary)["version"] as? String
            let url = (result as! NSDictionary)["url"] as? String
            let releaseNotes = (result["releaseNotes"] as? String)
            if  version != nil {
                let note = releaseNotes==nil ? "修复已知的bug":releaseNotes!
                let isok = AppInfo.majorVersion().isOlder(than: version!)
                if isok == true {
                    var info = VersionInfo()
                    info.version = version
                    info.url = url
                    info.message = note
                    VersionManager.versionUpdate(info: info)
                }
            }
            
        }) { (result) in
            show_msg("获取数据失败，检查您的网络，刷新请重试！".localized())
        }
    }
}
